package com.TcFingerClient;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.techshino.fp.client.Callback;
import com.techshino.fp.client.TcFingerClient;
import com.techshino.fp.entity.FingerResult;
import com.techshino.fp.util.SerialPortUtil;
import com.techshino.fp.util.SerialPortUtil.Protocol;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class TcFingerClientApi {
	
	private TcFingerClient mTcFingerClient;
	SerialPortUtil mSerialPortUtil;
	private String mPortPath;
	private int  mBaudrate;
	
	public TcFingerClientApi(Context context){
		
	
		mSerialPortUtil = SerialPortUtil.getInstance(context.getApplicationContext());
		mSerialPortUtil.setProtocol(Protocol.LASERX);
//		mTcFingerClient = mSerialPortUtil.openDevice(PortPath, Baudrate);
	}
	
	public int DLL_OpenDevice(String PortPath,int Baudrate){
		this.mPortPath = PortPath;
		this.mBaudrate = Baudrate;
		mTcFingerClient = mSerialPortUtil.openDevice(PortPath, Baudrate);
		if(mTcFingerClient != null){
			return 0;
		}
		return -1;
	}
	
	public int DLL_tcDetectFinger(){
		if(mTcFingerClient != null){
			
			return mTcFingerClient.tcDetectFinger();
		}
		
		return -1;
	}
	
	public void DLL_tcGetFeature(int timeout,Callback callback){
		
		if(mTcFingerClient != null){
			mTcFingerClient.tcGetFeature(timeout, callback);
		}

	}
	
	public void DLL_tcSetBaudRate(int baudRate,Callback callback){

		if(mTcFingerClient != null){
			mTcFingerClient.tcSetBaudRate(baudRate, callback);
		}
	}
	
	public void DLL_tcSetBaudRate(int baudRate){

		if(mTcFingerClient != null){
			mTcFingerClient.tcSetBaudRate(baudRate);
		}
	}
	
	public void DLL_tcCancel(){
		if(mTcFingerClient != null){
			mTcFingerClient.tcCancel();
		}
	}
	
	public FingerResult DLL_tcGetDevSn(){
		
		if(mTcFingerClient != null){
			return mTcFingerClient.tcGetDevSn();
		}
		return null;
	}
	
	public void DLL_tcGetTemplate(int nTimeout,Callback callback){
		
		if(mTcFingerClient != null){
			mTcFingerClient.tcGetTemplate(nTimeout, callback);
		}
	}
	
	public void DLL_tcGetImage(int nTimeout,Callback callback){
		
		if(mTcFingerClient != null){
			
			mTcFingerClient.tcSetBaudRate(7);
			mBaudrate = 115200;
			mTcFingerClient = mSerialPortUtil.openDevice(mPortPath, mBaudrate);
			mTcFingerClient.tcGetImage(nTimeout, callback);
		}
	}

	public void DLL_FingerRelease(){
		mSerialPortUtil.closeDevice();
	}
	
	public FingerResult DLL_tcMatch(byte[] feature,byte[] template){
		
		if(mTcFingerClient != null){
			return mTcFingerClient.tcMatch(feature, template);
		}
	
		return null;
	}
	
	   public static void gpio_control(int gpio,int mode){
			try
	        {
				String file_name = new String("/sys/class/gpio/gpio"+gpio+"/value");
				File file = new File(file_name);
				if(file.exists()==false){
					file_name = new String("/sys/class/gpiocontrol/gpiocontrol/gpiocontrol"+gpio);
					file = new File(file_name);
				}
				
				FileWriter localFileWriter = new FileWriter(file);
			    if(mode==1)
			       localFileWriter.write("1");
			    else if(mode==0)
			       localFileWriter.write("0");
			    localFileWriter.close();
		    }
	        catch (IOException localIOException)
	        {
	            localIOException.printStackTrace();
	        }
		}
	    public static boolean gpio_read(int gpio){
			try
	        {
				String file_name = new String("/sys/class/gpio/gpio"+gpio+"/value");
				File file = new File(file_name);
				if(file.exists()==false){
					file_name = new String("/sys/class/gpiocontrol/gpiocontrol/gpiocontrol"+gpio);
					file = new File(file_name);
				}
				
				FileReader localFileRead = new FileReader(file);
				char[] ch = new char[1];
				localFileRead.read(ch);
				localFileRead.close();
				if(ch[0]=='1') return true;
				return false;
				
		    }
	        catch (IOException localIOException)
	        {
	            localIOException.printStackTrace();
	        }
			return false;
		}
	public void clear() {
		while(true){
			int avalible = mSerialPortUtil.avalible();
			if (avalible > 0) {
				Log.d("mSerialPortUtil","mSerialPortUtil:"+avalible);
				byte[] data = new byte[avalible];
				mSerialPortUtil.read(data, 0, data.length);

			}
			else break;
		}
	}
	
}
